import moment from "moment";

const Util = {};

Util.install = function (Vue, data) {
  const Datas = { ...data };

  /**
   * 获取前一周的开始时间，结束时间，以及周数
   * @param {周} week
   * @returns
   */
  Datas.getPreWeek = function (week) {
    if (isParamBlank(week)) {
      return;
    }
    var startDate = moment()
      .week(week - 1)
      .startOf("week")
      .add(1, "days")
      .format("YYYY-MM-DD"); //这样是年月日的格式
    var endDate = moment()
      .week(week - 1)
      .endOf("week")
      .add(1, "days")
      .format("YYYY-MM-DD");
    var weekCount = moment(moment(startDate).subtract(1, "days")).weeks();
    var preWeekInfo = {
      startDate: startDate,
      endDate: endDate,
      weekCount: weekCount,
    };
    return preWeekInfo;
  };

  /**
   * 获取下一周的起始日期和结束日期,以及周数
   * @param {周} week
   * @returns
   */
  Datas.getNextWeek = function (week) {
    if (isParamBlank(week)) {
      return;
    }
    var startDate = moment()
      .week(week + 1)
      .startOf("week")
      .add(1, "days")
      .format("YYYY-MM-DD"); //这样是年月日的格式
    var endDate = moment()
      .week(week + 1)
      .endOf("week")
      .add(1, "days")
      .format("YYYY-MM-DD");
    var weekCount = moment(moment(startDate).subtract(1, "days")).weeks();
    var nextWeekInfo = {
      startDate: startDate,
      endDate: endDate,
      weekCount: weekCount,
    };
    return nextWeekInfo;
  };

  /**
   * 根据传入参数，获取周次信息
   * @param {*} param
   * @returns
   */
  Datas.getWeekInfoByDate = function (param) {
    if (isParamBlank(param)) {
      // 当前时间所在周数
      return;
    }
    var date = moment(param);
    var startDate = moment()
      .week(moment(date).week())
      .startOf("week")
      .add(1, "days")
      .format("YYYY-MM-DD"); //这样是年月日的格式
    var endDate = moment()
      .week(moment(date).week())
      .endOf("week")
      .add(1, "days")
      .format("YYYY-MM-DD");
    var weekCount = moment(moment(startDate).subtract(1, "days")).weeks();
    // 获取当前的周次 , 如果不穿参数，则表示当前的周数 ,如果传入时间，则表示 该时间段所在的周数
    //因为是基于moment.js  起始时间还是从周日开始算的 ,所以需要在现有的基础上减去一天 ，这样也是为了防止边界情况出错
    var weekInfo = {
      startDate: startDate,
      endDate: endDate,
      weekCount: weekCount,
    };
    return weekInfo;
  };

  /**
   * 获取一年中的总周次
   * @returns 次数
   */
  Datas.getWeekTotalInYear = function () {
    return moment().weeksInYear();
  };

  /**
   * 校验参数是否为空
   * @param {参数} param
   * @returns 是否为空
   */
  Datas.isParamBlank = function (param) {
    if (typeof param == "undefined" || param == null || param.length == 0) {
      return true;
    }
    return false;
  };

  /**
   * 获取当前周的周数以及该周的开始时间和结束时间
   * @returns
   */
  Datas.getCurrentWeekInfo = function () {
    // var weekCount = moment( moment(moment().week(moment().week()).startOf('week')).subtract(1,'days')).weeks();
    var weekCount = moment(moment().subtract(1, "days")).weeks();
    var startDate = moment()
      .week(weekCount)
      .startOf("week")
      .add(1, "days")
      .format("YYYY-MM-DD"); //这样是年月日的格式
    var endDate = moment()
      .week(weekCount)
      .endOf("week")
      .add(1, "days")
      .format("YYYY-MM-DD");
    var currentWeekInfo = {
      startDate: startDate,
      endDate: endDate,
      weekCount: weekCount,
    };
    return currentWeekInfo;
  };

  Datas.getDayByWeekDay = function (week, weekNum) {
    if (isParamBlank(week) && isParamBlank(weekNum)) {
      return;
    }
    return moment()
      .week(week)
      .startOf("week")
      .add(weekNum, "days")
      .format("YYYY-MM-DD"); //这样是年月日的格式
  };

  //为了解决点击上一周和下一周的 边界值情况，新增以下两个函数

  //点击上一周
  Datas.clickPreIconGetPreWeek = function (week, currentWeekStartTime) {
    if (isParamBlank(week) && isParamBlank(currentWeekStartTime)) {
      return;
    }
    var startDate = moment(currentWeekStartTime)
      .subtract(7, "days")
      .format("YYYY-MM-DD");
    var endDate = moment(startDate).add(6, "days").format("YYYY-MM-DD");
    var weekCount = moment(moment(startDate).subtract(1, "days")).weeks();
    var preWeekInfo = {
      startDate: startDate,
      endDate: endDate,
      weekCount: weekCount,
    };
    return preWeekInfo;
  };

  //点击下一周
  Datas.clickNextIconGetNextWeek = function (
    week,
    currentWeekStartTime,
    currentWeekEndTime
  ) {
    if (isParamBlank(week) && isParamBlank(currentWeekEndTime)) {
      return;
    }
    var startDate = moment(currentWeekStartTime)
      .add(7, "days")
      .format("YYYY-MM-DD");
    var endDate = moment(startDate).add(6, "days").format("YYYY-MM-DD");
    var weekCount = moment(moment(startDate).subtract(1, "days")).weeks();
    var nextWeekInfo = {
      startDate: startDate,
      endDate: endDate,
      weekCount: weekCount,
    };
    return nextWeekInfo;
  };

  Vue.prototype.$dateUtil = Datas; // 挂在原型上，方便使用
};

export default Util;
